
define(['angular',"text!www/trends/collect.html",'url','common'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($scope,$rootScope,$http){
	   showSpin();
		rightMenu();
		var collect = function () {
			this.my_collect = [];
			this.baseUrl = ip+"/";
			this.adUrl = ip+"/"+"data/afficheimg/";
			this.busy = false;
			this.wxid = 0;
	    };
		$scope.collect = new collect();
		$scope.collect.initCollect = function(){
		   	 if (this.busy){
		       	 hideSpin();
		       	 return;
		        }
		    this.busy = true;
		    var pro = this;
	   	 var url = cl.getMyCollectUrl+"&callback=JSON_CALLBACK" ;
	        $http.jsonp(url).success(function (res) {
					if(res.code=="CL02024"){//获取成功
						pro.my_collect = res.result.res;
						 if(pro.my_collect.length==0){
				      		   $rootScope.changeNull();
				      	  }else{
				      		  $rootScope.hideNull();
				      	  }
						 pro.wxid = res.result.wxid;
					}else{
						showAlert("请登录后再次进入");
						$rootScope.changeNull();
//						$rootScope.go('back', 'fade');
					}
					hideSpin();
					pro.busy = false;
	        });
	    };
	    $scope.collect.collectNews = function(news_id,index){
	   	 if (this.busy){
	       	 return;
	        }
	   	 if(!this.wxid>0)
	        {
	   		 showAlert("登录后方可操作");
	   		 return;
	        }
	   	 this.busy = true;
	   	 var pro = this;
	        var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
	        $http.jsonp(url).success(function (res) {
					if(res.code=="CL02026"){//获取成功
						if(res.result.codeMsg=="cancel"){
							showAlert("取消收藏");
							pro.my_collect[index].is_collect = "重新关注";
						}else{
							showAlert("已加入收藏");
							pro.my_collect[index].is_collect = "移除关注";
						}
					}else{
						showAlert("收藏失败，请登录后再操作");
					}
					pro.busy = false;
	        });
	    };
	    
	    $scope.collect.likeNew = function(news_id,index){
	    	if(!this.wxid){
	    		showAlert("请登录后方可点赞");
	    		return false;
	    	}
	    	if (this.busy){
		       	 hideSpin();
		       	 return;
		        }
		    this.busy = true;
		    var pro = this;
		    var url = cl.likeNewUrl+"&wxid="+this.wxid +"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
		    $http.jsonp(url).success(function (res) {
				if(res.code=="CL02036"){//获取成功
					if(res.result.codeMsg=="cancel"){
						pro.my_collect[index].like_num =  parseInt(pro.my_collect[index].like_num) - 1;
						pro.my_collect[index].is_like = "";
						showAlert("取消点赞");
					}else{
						pro.my_collect[index].like_num = parseInt(pro.my_collect[index].like_num) + 1;
						pro.my_collect[index].is_like = "liked";
						showAlert("成功点赞");
					}
				}else{
					showAlert("点赞失败，请登录后再操作");
				}
				pro.busy = false;
		    });
	    };
		$scope.collect.initCollect();
    };
    return {controller:controller,tpl:tpl};
});
